package com.sinoglobal.dumping.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sinoglobal.dumping.R;
import com.sinoglobal.dumping.api.Dumpling_RemoteImpl;
import com.sinoglobal.dumping.base.Dumpling_NetWorkUtil;
import com.sinoglobal.dumping.base.Dumpling_ShareAbstractActivity;
import com.sinoglobal.dumping.base.Dumpling_SinoAsyncTask;
import com.sinoglobal.dumping.base.Dumpling_SinoConfig;
import com.sinoglobal.dumping.base.Dumpling_SinoConstans;
import com.sinoglobal.dumping.bean.Dumpling_ShareInfoResultList;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyDumplingBean;
import com.sinoglobal.dumping.bean.myDumpling.Dumpling_MyDumplingBean.MyDumpling;
import com.sinoglobal.dumping.util.Dumpling_HttpMethodSet;
import com.sinoglobal.dumping.view.Dumpling_RefreshScrollView;
import com.sinoglobal.dumping.view.Dumpling_RefreshScrollView.OnRefreshListener;
import com.sinoglobal.wallet.activity.W_BalanceDetailActivity;
import com.sinoglobal.wallet.activity.W_CouponActivity;


public class Dumpling_MyDumplingActivity extends Dumpling_ShareAbstractActivity {
	
	// 我包  现金   贺卡个数
	private TextView tvCash2 , tvCardNum2;
	// 捞到   包的 饺子数 
    private TextView tvDumplingNums , tvMakeDumplingNums;
	//  我捞   现金    优惠券个数  贺卡个数
    private TextView tvCash , tvCouponNum , tvCardNum ,tvTicketNum;
    private Button btnShowOff;
    private View hasDataView , noDataView , loadDataErrorView , hasNetworkConnectedView;

	private Dumpling_RefreshScrollView sv_refresh_view;

	private LinearLayout ll_scrollview_child;
	private boolean isRefreshed = true ;  // 默认 没有开启刷新
	private MyDumpling myDumpling;
    
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		
		  View view = getView(R.id.title_right_btn_share);
	      hideView(mTemplateRightText, mTemplateRightImg,view);
	      mTemplateTitleText.setText("我的饺子");
	      mTemplateLayout.setBackgroundColor(getResources().getColor(R.color.dumpling_red3));
	      setContentView(R.layout.dumpling_activity_my_dumpling);
	       
	      init();
	}
	
	private void init() {
		 initScrollView();
		 initView();
		 initData(); // 首次加载数据
	}
	
	private void initScrollView() {
		sv_refresh_view =  (Dumpling_RefreshScrollView) findViewById(R.id.sv_refresh_view);
		
		ll_scrollview_child = (LinearLayout) View.inflate(this, R.layout.dumpling_view_scrollview_child, null);
		
		sv_refresh_view.addChild(ll_scrollview_child, 1);
		
		if (isRefreshEnable()) {
			sv_refresh_view.setonRefreshListener(new OnRefreshListener() {
				
				@Override
				public void onRefresh() {
					isRefreshed = false ;
					initData();
				}
				
			} );
		}
	}

	private void initView() {
		
		hasDataView = findViewById(R.id.id_exist_fragment);
		noDataView = findViewById(R.id.id_no_fragment);
		loadDataErrorView = findViewById(R.id.id_load_data_errorview);
		hasNetworkConnectedView = findViewById(R.id.id_network_has_connected);
		
		 tvDumplingNums = (TextView) ll_scrollview_child.findViewById(R.id.tv_dumpling_nums);
         tvCash = (TextView) ll_scrollview_child.findViewById(R.id.tv_cash_num);
         tvCardNum = (TextView) ll_scrollview_child.findViewById(R.id.tv_card_num);
         tvCouponNum = (TextView) ll_scrollview_child.findViewById(R.id.tv_coupon_num);
         tvTicketNum = (TextView) ll_scrollview_child.findViewById(R.id.tv_ticket_num);
         tvMakeDumplingNums = (TextView) ll_scrollview_child.findViewById(R.id.tv_make_dumpling_nums);
         tvCash2 = (TextView) ll_scrollview_child.findViewById(R.id.tv_cash_num2);
         tvCardNum2 = (TextView) ll_scrollview_child.findViewById(R.id.tv_card_num2);
         
         LinearLayout ll_my_fixup_cash_dumpling = (LinearLayout) ll_scrollview_child.findViewById(R.id.ll_my_fixup_cash_dumpling);
         LinearLayout ll_my_fixup_card_dumpling = (LinearLayout) ll_scrollview_child.findViewById(R.id.ll_my_fixup_card_dumpling);
         LinearLayout ll_my_fixup_coupon_dumpling = (LinearLayout) ll_scrollview_child.findViewById(R.id.ll_my_fixup_coupon_dumpling);
         LinearLayout ll_my_make_cash_dumpling = (LinearLayout) ll_scrollview_child.findViewById(R.id.ll_my_make_cash_dumpling);
         LinearLayout ll_my_make_card_dumpling = (LinearLayout) ll_scrollview_child.findViewById(R.id.ll_my_make_card_dumpling);
         
         
         btnShowOff = (Button) ll_scrollview_child.findViewById(R.id.btn_show_off);
         btnShowOff.setOnClickListener(Dumpling_MyDumplingActivity.this);
         
         loadDataErrorView.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	            	 initData();
	            }
	        });
         
         hasNetworkConnectedView.setOnClickListener(new View.OnClickListener() {
	            @Override
	            public void onClick(View v) {
	            	initData();
	            }
	        });
		
         ll_my_fixup_cash_dumpling.setOnClickListener(this);
         ll_my_fixup_card_dumpling.setOnClickListener(this);
         ll_my_fixup_coupon_dumpling.setOnClickListener(this);
         ll_my_make_cash_dumpling.setOnClickListener(this);
         ll_my_make_card_dumpling.setOnClickListener(this);
         
	}
	 
	/**
	 * 是否启用刷新
	 * 
	 * @return
	 */
	private boolean isRefreshEnable() {
		return false;
	}
	
    /**
     *  请求数据
     */
	private void initData() {
		 queryMydumpling();
	}
    
	    /**
	     * 查询我的饺子信息
	     */
	private void queryMydumpling() {
	      new Dumpling_SinoAsyncTask<Object, Void, Dumpling_MyDumplingBean>(this, false,true) {

				@Override
	            public Dumpling_MyDumplingBean before(Object... params) throws Exception {
	                return Dumpling_RemoteImpl.getInstance().queryMydumpling(Dumpling_SinoConstans.USER_ID);
	            }

	            @Override
	            public void after(Dumpling_MyDumplingBean data) {
	            	if (!isRefreshed) {
						onRefreshFinish();
					}
	            	
	            	if (data != null && data.resultList != null ) {
						
	            		myDumpling = data.resultList;
                        
	            		// 有 捞的 或者 包的饺子
	            		if (myDumpling.putDumpNumber > 0 || myDumpling.count > 0) {
	            			 showView(hasDataView);
	                         hideViewForGone(noDataView ,loadDataErrorView , hasNetworkConnectedView);
	            			 Dumpling_HttpMethodSet.getShareContent(Dumpling_MyDumplingActivity.this, Dumpling_MainActivity.TEMP_ID_STRUT, "", "", "", String.valueOf(myDumpling.price));
	                         tvDumplingNums.setText(myDumpling.count + "");
	                         tvMakeDumplingNums.setText(myDumpling.putDumpNumber + "");
	                         tvCash.setText(myDumpling.price + "");
	                         tvCardNum.setText(myDumpling.cardNumber + "");
	                         tvCouponNum.setText(myDumpling.couponNumber + "");
//	                         tvTicketNum.setText(0+"");
	                         tvCash2.setText(myDumpling.putMoney + "");
	                         tvCardNum2.setText(myDumpling.putCardNumber + "");
						} else { // 没有数据
							hideViewForGone(hasDataView , loadDataErrorView , hasNetworkConnectedView);
                        	showView(noDataView);
						}
	            		
					} else {
						onLoadDataError();
					}
	            }

	            @Override
	            public void exception() {
	            	onLoadDataError();

	            }
	        }.execute();
	    }
	/**
	 * 加载数据异常
	 */
	private void onLoadDataError() {
		if (Dumpling_NetWorkUtil.getNetWork(Dumpling_MyDumplingActivity.this)) {  // 没有网络
			hideViewForGone(hasDataView , noDataView ,loadDataErrorView );
            showView(hasNetworkConnectedView);
		} else { //  服务器异常
			hideViewForGone(hasDataView , noDataView , hasNetworkConnectedView);
			showView(loadDataErrorView);
		}
	}

	/**
	 *  下拉刷新完成
	 */
	private void onRefreshFinish() {
		sv_refresh_view.onRefreshComplete();
		isRefreshed = true ;
	}

	@Override
	public void onAntiViolence(View v) {
		if(v.getId()==R.id.btn_show_off){
			if (Dumpling_SinoConfig.sStrutShare != null) {
				Dumpling_HttpMethodSet.markShare(this, Dumpling_SinoConstans.USER_ID);
				Dumpling_ShareInfoResultList result = Dumpling_SinoConfig.sStrutShare.getResultList();
				this.setShare(result.getContent(), result.getTitle(), null, result.getUrl()+"?productCode="+ Dumpling_SinoConstans.PRODUCT_CODE, 0, result.getPicurl());
			}
		}else if(v.getId()==R.id.ll_my_fixup_cash_dumpling){
			try {
				goIntent(W_BalanceDetailActivity.class);// 我捞 现金

		        } catch (Exception e) {
		            e.printStackTrace();
		        }
		}else if(v.getId()==R.id.ll_my_fixup_card_dumpling){
			showToast("敬请期待");
//			Intent intent=new Intent(Dumpling_MyDumplingActivity.this,Dumpling_ReceivedCardActivity.class);
//			intent.putExtra("cardNumber",myDumpling.cardNumber);
//			startActivity(intent);
		}else if(v.getId()==R.id.ll_my_fixup_coupon_dumpling){
			Intent intent = new Intent();
			intent.setClass(Dumpling_MyDumplingActivity.this, W_CouponActivity.class);
			startActivity(intent);
		}else if(v.getId()==R.id.ll_my_make_cash_dumpling){// 我包  现金
				
			Bundle bundle = new Bundle();
			bundle.putDouble("putMoney", myDumpling.putMoney ); // 现金饺子 总金额
			bundle.putInt("putMoneyNumber", myDumpling.putMoneyNumber); // 现金饺子 总数量
			goIntent(Dumpling_MakeCashDumplingListActivity.class ,bundle);
		}else if(v.getId()==R.id.ll_my_make_card_dumpling){
			showToast("敬请期待");
//			Intent intent=new Intent(Dumpling_MyDumplingActivity.this,Dumpling_MakeCardListActivity.class);
//			intent.putExtra("cardNumber",myDumpling.putCardNumber);
//			startActivity(intent);
		} 
	}

}
